package com.human.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.Assessments;
import com.human.server.model.R;
import com.human.server.model.request.AssessmentsAddRequest;
import com.human.server.model.request.AssessmentsRequest;
import com.human.server.service.impl.AssessmentsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考核管理
 */
@RestController
@RequestMapping("/human/assessments")
public class AssessmentsController {

    @Autowired
    private AssessmentsServiceImpl assessmentsService;

    /**
     * 分页查询
     * @param assessmentsRequest
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody AssessmentsRequest assessmentsRequest) {
        return R.success(assessmentsService.page(assessmentsRequest, new LambdaQueryWrapper<Assessments>()
                .like(StrUtil.isNotEmpty(assessmentsRequest.getName()), Assessments::getName, assessmentsRequest.getName())
                .eq(StrUtil.isNotEmpty(assessmentsRequest.getStatus()), Assessments::getStatus, assessmentsRequest.getStatus())
                .orderByDesc(Assessments::getCreatedAt)));
    }

    /**
     * 新增
     * @param assessmentsAddRequest
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody AssessmentsAddRequest assessmentsAddRequest) {

        assessmentsService.add(assessmentsAddRequest);
        return R.success();
    }

    /**
     * 修改
     * @param assessmentsAddRequest
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody AssessmentsAddRequest assessmentsAddRequest) {
        assessmentsService.updateAssessments(assessmentsAddRequest);
        return R.success();
    }

    /**
     * 删除
     * @param assessments
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Assessments assessments) {
        return R.success(assessmentsService.removeById(assessments.getId()));
    }

    /**
     * 详情
     * @param assessments
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody Assessments assessments) {
        return R.success(assessmentsService.getById(assessments.getId()));
    }

    /**
     * 列表
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Assessments assessments) {
        return R.success(assessmentsService.list(new LambdaQueryWrapper<Assessments>()
                .eq(assessments.getStatus() != null, Assessments::getStatus, assessments.getStatus())
                .eq(assessments.getDepartment() != null, Assessments::getDepartment, assessments.getDepartment())
                .orderByDesc(Assessments::getCreatedAt)
        ));
    }

    /**
     * 查询考核步骤
     */
    @PostMapping("/steps")
    public R steps(@RequestBody Assessments assessments) {
        return R.success(assessmentsService.steps(assessments));
    }

    /**
     * kpis
     */
    @PostMapping("/kpis")
    public R kpis(@RequestBody Assessments assessments) {
        return R.success(assessmentsService.kpis(assessments));
    }

    /**
     * 发布
     */
    @PostMapping("/publish")
    public R publish(@RequestBody Assessments assessments) {

        assessmentsService.publish(assessments);
        return R.success();
    }

}
